package GDoc::Job::Fetch::US::Exec::EOP::OMB::HistTables;

use Moose;

use WWW::Mechanize;
use GDoc::Job;
use File::Temp;


extends 'GDoc::Job::Fetch::US::Exec::EOP';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.whitehouse.gov/omb/budget/Historicals/' );
    $self->_basedir( 'http://www.whitehouse.gov' );
}

sub run { 
    my $self = shift;
   
    if ( $self->data->{link} ) { 
        $self->_fetch_xls;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );

        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        my @links = $mech->find_all_links( url_regex => qr{hist\d{2}z\d\.xls$}i );

        foreach my $link( @links ) { 
            $link->url =~ m{hist(?<chap>\d{2})z(?<sec>\d)};
            my ( $chap, $sec ) = @+{ 'chap', 'sec' };

            my $title = $link->text;
            $title =~ s{\x{2014}|\x{2013}}{-}g;      # unicode dashes will break SQS
            $title =~ s{^Table\s\d{1,2}\.\d{1}.}{};

            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::EOP::OMB::HistTables',
                                      queue => $self->fetch_queue,
                                      data  => { link    => $link->url,
                                                 title   => $title,
                                                 chap    => $chap,
                                                 sec     => $sec
                                               }
                                    );
            
            $job->submit;
        }
    }
}



sub _fetch_xls { 
    my $self = shift;
    my %data = %{ $self->data };

    my $ident = sprintf( 'OMB-HT-%02d-%02d', $data{chap}, $data{sec} );
    my $filename = $ident . '.xls';
    my $key = 'us/exec/eop/omb/' . $filename;

    my $bkt = $self->s3->bucket( $self->docs_bucket );
    if ( $bkt->head_key( $key ) ) { 
        $self->logger->info( "Already have $data{link}, skipping" );
        return;
    }

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    $mech->get( $self->_basedir . $data{link} );

    $self->store_xls( key       => $key,
                      data      => $mech->response->content,
                      filename  => $filename,
                      meta      => { description  => $data{title},
                                     identifier   => $ident
                                   }
                    );               
}

1;
